package com.ruoyi.web.controller.personnel;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.R;
import com.ruoyi.personnel.service.LikeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.service.IEmployeeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 员工管理Controller
 *
 * @author ruoyi
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/personnel/employee")
public class EmployeeController extends BaseController
{
    @Autowired
    private IEmployeeService employeeService;


    // 缓存需要回滚的信息  TODO 更改为redis缓存技术
     static R r = new R();

    /**
     * 查询员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(Employee employee)
    {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }

    /**
     * 导出员工管理列表
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:export')")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Employee employee)
    {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, list, "员工管理数据");
    }

    /**
     * 获取员工管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:query')")
    @GetMapping(value = "/{empId}")
    public AjaxResult getInfo(@PathVariable("empId") Integer empId)
    {
        return success(employeeService.selectEmployeeByEmpId(empId));
    }

    /**
     * 新增员工管理
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:add')")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Employee employee)
    {
        return toAjax(employeeService.insertEmployee(employee));
    }

    /**
     * 修改员工管理
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:edit')")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Employee employee)
    {
        return toAjax(employeeService.updateEmployee(employee));
    }

    /**
     * 删除员工管理
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:remove')")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{empIds}")
    public AjaxResult remove(@PathVariable Integer[] empIds)
    {

        for (Integer empId : empIds) {
            r.addList(empId);
        }
        return toAjax(employeeService.deleteEmployeeByEmpIds(empIds));
    }
    /**
     * 回滚
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:rollback')")
    @Log(title = "员工信息回滚", businessType = BusinessType.DELETE)
    @DeleteMapping
    public Boolean rollback()
    {
        List list = r.getList();

        if (list.size()==0){
            return false;
        }else {
            Object[] array = list.toArray();
            employeeService.rollbackEmployee(array);
            r.setList(new ArrayList<>());
            return true;
        }
    }




}
